package dev.nextcore.contoller

import jakarta.servlet.http.HttpServletRequest
import org.springframework.boot.web.servlet.error.ErrorController
import org.springframework.http.HttpStatus
import org.springframework.stereotype.Controller
import org.springframework.web.bind.annotation.RequestMapping

@Controller
class CustomErrorController : ErrorController {

    @RequestMapping("/error")
    fun handleError(request: HttpServletRequest): String {
        // 获取原始状态码
        val status = request.getAttribute("jakarta.servlet.error.status_code") as? Int

        return when (status) {
            HttpStatus.NOT_FOUND.value() -> "redirect:/"
            else -> "error" // 其他错误显示通用错误页
        }
    }
}